﻿using System;
using System.IO;
using System.Windows;
using System.Windows.Forms;
using System.Windows.Input;
using MagicKey.Components.UserManager;
using MagicKey.Factory;
using MagicKey.Interfaces;
using Microsoft.Win32;
using OpenFileDialog = Microsoft.Win32.OpenFileDialog;

namespace MagicKey.UserInterface
{
    /// <summary>
    /// Interaktionslogik für Windows2.xaml
    /// </summary>
    public partial class UserFiles : Window
    {
        #region Members

        private static readonly IMagicKey MagicKey = MagicKeyFactory.GetInstance().GetMagicKeyInterface();
        private MainWindow _mainWindow;
        #endregion

        public UserFiles()
        {
            InitializeComponent();
        }

        public UserFiles(MainWindow myWindow)
        {
            InitializeComponent();

            // The following line enables the tremble effect for the current
            // window when clicking on the main window
            this.Owner = myWindow;

            _mainWindow = myWindow;
        }

        private void button2_Click(object sender, RoutedEventArgs e)
        {
            var rechteverwalten = new ManageAuthorizations();
            rechteverwalten.Show();
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {

            // Create OpenFileDialog
            var openFileDialog = new OpenFileDialog
            {
                //// Set filter for file extension
                //DefaultExt = ".txt",
                //// default file extension
                //Filter = "Text documents (.txt)|*.txt"
            };


            // Display OpenFileDialog by calling ShowDialog method
            var result = openFileDialog.ShowDialog();

            // Get the selected file name and display in a TextBox
            if (result != true) return;

            var masterUser = _mainWindow.GetMasterUser();

            // Open document
            var database = _mainWindow.GetDatabase();
            var factory = _mainWindow.GetFactory();
            var magicKey = factory.GetMagicKeyInterface();
            var accessManager = magicKey.GetAccessManager();
            var fileAccess = magicKey.GetFileAccess();
            var userManager = magicKey.GetUserManager();

            //string k1 = accessManager.GenerateSysmKey();
            //Console.WriteLine(k1);
            //string k2 = accessManager.GenerateSysmKey();
            //Console.WriteLine(k2);
            //string k3 = accessManager.GenerateSysmKey();
            //Console.WriteLine(k3);
            //string k4 = accessManager.GenerateSysmKey();
            //Console.WriteLine(k4);

            accessManager.EncryptFileSym(openFileDialog.FileName, userManager.GetUserSymetricKey(masterUser));

            openFileDialog = new OpenFileDialog
            {
                //// Set filter for file extension
                //DefaultExt = ".txt",
                //// default file extension
                //Filter = "Text documents (.txt)|*.txt"
            };
            // Display OpenFileDialog by calling ShowDialog method
            result = openFileDialog.ShowDialog();

            // Get the selected file name and display in a TextBox
            if (result != true) return;

            accessManager.DecryptFileSym(openFileDialog.FileName, userManager.GetUserSymetricKey(masterUser));
            
        }



        private void button3_Click(object sender, RoutedEventArgs e)
        {

        }
    }
}
